package com.ych.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ych.beans.Message;
import com.ych.beans.Perm;
import com.ych.beans.Role;
import com.ych.dao.RoleDao;
import com.ych.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.TreeSet;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao dao;
    @Override
    public Message selectRole(Integer pageNo, String roleName) {
        Message message = new Message();
        if(pageNo!=null){
            PageHelper.startPage(pageNo, 8);
        }
        List<Role> list=dao.selectRole(roleName,null);
        final PageInfo<Role> objectPageInfo = new PageInfo<>(list);
        if(!list.isEmpty()){
            message.setSuccess(true);
            message.setObj(objectPageInfo);
        }
        return message;
    }

    @Override
    public Message addRole(Role role) {
        Message message = new Message();
        boolean flag = dao.addRole(role);
        message.setSuccess(flag);
        if (flag){
            message.setMsg("添加成功");
        }else {
            message.setMsg("添加失败");
        }
        return message;
    }

    @Override
    public Message deleteRole(Integer id) {
        final Message message = new Message();
        boolean flag = dao.deleteRole(id);
        message.setSuccess(flag);
        if (flag){
            message.setMsg("删除成功");
        }else {
            message.setMsg("删除失败");
        }
        return message;
    }

}
